package com.yidu.drug.action;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Page;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.FdSellDetailModel;
import com.yidu.drug.model.FinancialRecordModel;
import com.yidu.drug.model.FinancialSheetsModel;
import com.yidu.drug.model.InventoryModel;
import com.yidu.drug.model.InventoryRecordModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.model.ReturnsDetailModel;
import com.yidu.drug.model.SalesReturnModel;
import com.yidu.drug.service.FinancialRecordService;
import com.yidu.drug.service.FinancialSheetsService;
import com.yidu.drug.service.InventoryRecordService;
import com.yidu.drug.service.InventoryService;
import com.yidu.drug.service.RepertoryService;
import com.yidu.drug.service.ReturnsDetailService;
import com.yidu.drug.service.SalesReturnService;

/** 退货单的Action
 * @author meilina
 * 2018年8月9日上午11:29:45
 * @version 1.0
 */
@Controller
@RequestMapping("/salesReturn")
public class SalesReturnAction {
	/**
	 * 退货Service
	 */
	@Resource
	private SalesReturnService saleReturnService;
	/**
	 * 退货详情Service
	 */
	@Resource
	private ReturnsDetailService returnsDetailService;
	/**
	 * 财务Service
	 */
	@Resource
	private FinancialSheetsService financialSheetsService;
	/**
	 * 财务记录Service
	 */
	@Resource
	private FinancialRecordService financialRecordService;
	/**
	 * 库存Service
	 */
	@Resource
	private RepertoryService repertoryService;
	/**
	 * 库存记录Service
	 */
	@Resource
	private InventoryRecordService inventoryRecordService;
	
	/**
	 * 查询所有和条件查询分页--退货单表
	 * @param response 响应
	 * @param reCause 传入参数退货原因
	 * @param reOper 传入参数操作人
	 * @param page 传入参数页面
	 * @param limit 传入分页
	 * @return maps
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String, Object> findAll(HttpServletResponse response,String reCause,String reOper,int page,int limit) {
		System.out.println("进入findAll方法");
		response.setCharacterEncoding("UTF-8");
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		Map<String,String> map=new HashMap<>();
		//根据条件查询的搜索框
		map.put("reCause", reCause);
		map.put("reOper", reOper);
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getRows()));
		List<SalesReturnModel> list = saleReturnService.findAllPage(map);
		for(SalesReturnModel salesReturnModel:list){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String reDates = sdf.format(salesReturnModel.getReDate()   );
			salesReturnModel.setReDates(reDates);
		}
		Map<String, Object> maps = new HashMap<>();
		SalesReturnModel salesReturn = new SalesReturnModel();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", saleReturnService.findTotalRows(salesReturn));
		maps.put("data", list);
			
		return maps;
	}
	
	
	/**
	 * 查询所有和条件查询分页--分店销售明细表
	 * @param response 响应
	 * @param me 会员对象
	 * @param page 页数
	 * @param limit 行数
	 * @return
	 */
	@RequestMapping("/findAllFdSellDetail")
	@ResponseBody
	public Map<String, Object> findAllFdSellDetail(HttpServletResponse response,String reCause,String sellId ,String reOper,int page,int limit) {
		System.out.println("进入findAllFdSellDetail方法");
		response.setCharacterEncoding("UTF-8");
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		Map<String, String> map=new HashMap<>();
		//根据条件查询的搜索框
		map.put("reCause", reCause);
		map.put("reOper", reOper);
		map.put("sellId", sellId);
		//map.put("reOper", reOper);
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getRows()));
		
		List<FdSellDetailModel> list = saleReturnService.findAllFdSellDetail(map);
		
		

		Map<String, Object> maps = new HashMap<>();
		FdSellDetailModel fdSellDetail = new FdSellDetailModel();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", saleReturnService.findCount(fdSellDetail));
		maps.put("data", list);
			
		return maps;
	}
	@RequestMapping("/findAllDrug")
	@ResponseBody
	public List<DrugModel> findAllDrug(DrugModel drugModel){
		return saleReturnService.findAllDrug(drugModel);
		
	}
	/**
	 * 查找药品名名称
	 * @param salesReturnModel
	 * @return
	 */
	@RequestMapping("/findDrugName")
	@ResponseBody
	public List<SalesReturnModel> findDrugName(String drugName, String mxQuantity, String sellDate ){
		return saleReturnService.findDrugName(drugName, mxQuantity, sellDate);
		
	}
	
	/**
	 * 增加和修改
	 * @param session web存储空间
	 * @param me 会员对象
	 * @return 页面跳转
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(SalesReturnModel salesReturn,BigDecimal totalMoney,String reCause) {
		System.out.println("进入到增加方法");
		String reId = UUID.randomUUID().toString();
		Timestamp reDate = new Timestamp(new Date().getTime());
		
		salesReturn.setReId(reId);
		salesReturn.setReDate(reDate);
		salesReturn.setReCause(reCause);
		salesReturn.setReMoney(totalMoney);
		salesReturn.setIsva("否");
		salesReturn.setSort(1);
		salesReturn.setFilId("1");
		
		
		
		int rows = saleReturnService.save(salesReturn);
		/*ReturnsDetailModel  returnsDetailModel = new ReturnsDetailModel();
		returnsDetailModel.setReId(reId);
		returnsDetailModel.setDrugId(drugId);
		returnsDetailModel.setRdNumber(Long.valueOf(salesReturnQuantity));
		returnsDetailModel.setRdTotal(totalMoney);
		returnsDetailModel.setOptime(reDate);
		
		returnsDetailService.insertSelective(returnsDetailModel);*/
		
		System.out.println("==========="+rows);
		if(rows>0){
			return "ok";
			
		}else{
			return "no";
	
		}
		
	}
	
	
}